package com.ruoyi.web.controller.platform;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.platform.domain.MMonitoringEquipment;
import com.ruoyi.platform.mapper.MStoreLocationMapper;
import com.ruoyi.platform.service.IMMonitoringEquipmentService;
import com.ruoyi.platform.service.IMStoreService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.MStoreLocation;
import com.ruoyi.platform.service.IMStoreLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【仓位管理】Controller
 *
 * @author dataControl
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/platform/location")
public class MStoreLocationController extends BaseController {
    @Autowired
    private IMStoreLocationService mStoreLocationService;

    @Autowired
    private IMStoreService storeService;

    @Autowired
    private IMMonitoringEquipmentService monitoringEquipmentService;

    /**
     * 查询【仓位信息】列表
     */
    @PreAuthorize("@ss.hasPermi('platform:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(MStoreLocation mStoreLocation) {
        startPage();
        List<MStoreLocation> list = mStoreLocationService.selectMStoreLocationList(mStoreLocation);
        return getDataTable(list);
    }

    /**
     * 导出【仓位信息】列表
     */
    @PreAuthorize("@ss.hasPermi('platform:location:export')")
    @Log(title = "【仓位管理】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MStoreLocation mStoreLocation) {
        List<MStoreLocation> list = mStoreLocationService.selectMStoreLocationList(mStoreLocation);
        ExcelUtil<MStoreLocation> util = new ExcelUtil<MStoreLocation>(MStoreLocation.class);
        util.exportExcel(response, list, "【仓位管理】数据");
    }

    /**
     * 获取【仓位信息】详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:location:query')")
    @GetMapping(value = "/{storeLocationId}")
    public AjaxResult getInfo(@PathVariable("storeLocationId") Long storeLocationId) {
        return success(mStoreLocationService.selectMStoreLocationByStoreLocationId(storeLocationId));
    }

    /**
     * 新增【仓位信息】
     */
    @PreAuthorize("@ss.hasPermi('platform:location:add')")
    @Log(title = "【仓位管理】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MStoreLocation mStoreLocation) {
        if (mStoreLocation == null) {
            return AjaxResult.error("仓位信息不存在");
        }

        if (mStoreLocation.getEquipmentId() == null) {
            return AjaxResult.error("设备信息不存在");
        }

        MMonitoringEquipment item = monitoringEquipmentService.selectMMonitoringEquipmentByEquipmentId(mStoreLocation.getEquipmentId());
        if (item == null) {
            return AjaxResult.error("设备信息不存在");
        }

        if (item.getDelFlag().equals(1L)) {
            return AjaxResult.error("设备信息不存在");
        }

        mStoreLocation.setStoreId(item.getStoreId());
        return toAjax(mStoreLocationService.insertMStoreLocation(mStoreLocation));
    }

    /**
     * 修改【仓位信息】
     */
    @PreAuthorize("@ss.hasPermi('platform:location:edit')")
    @Log(title = "【仓位管理】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MStoreLocation mStoreLocation) {
        if (mStoreLocation == null) {
            return AjaxResult.error("仓位信息不存在");
        }

        MStoreLocation existItem=mStoreLocationService.selectMStoreLocationByStoreLocationId(mStoreLocation.getStoreLocationId());

        if (mStoreLocation.getDelFlag().equals(1L)) {
            return AjaxResult.error("仓位信息不存在");
        }

        if (mStoreLocation.getDelFlag().equals(1L)) {
            return AjaxResult.error("仓位信息不存在");
        }

        if (mStoreLocation.getEquipmentId() == null) {
            return AjaxResult.error("设备信息不存在");
        }

        MMonitoringEquipment item = monitoringEquipmentService.selectMMonitoringEquipmentByEquipmentId(mStoreLocation.getEquipmentId());
        if (item == null) {
            return AjaxResult.error("设备信息不存在");
        }

        if (item.getDelFlag().equals(1L)) {
            return AjaxResult.error("设备信息不存在");
        }

        mStoreLocation.setStoreId(item.getStoreId());
        return toAjax(mStoreLocationService.updateMStoreLocation(mStoreLocation));
    }

    /**
     * 删除【仓位信息】
     */
    @PreAuthorize("@ss.hasPermi('platform:location:remove')")
    @Log(title = "【仓位管理】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeLocationIds}")
    public AjaxResult remove(@PathVariable Long[] storeLocationIds) {
        return toAjax(mStoreLocationService.deleteMStoreLocationByStoreLocationIds(storeLocationIds));
    }
}
